package com.atguigu.interceptor;

import com.atguigu.entity.UserInfo;
import com.atguigu.result.Result;
import com.atguigu.result.ResultCodeEnum;
import com.atguigu.util.WebUtil;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 访问controller钱拦截，判断是否登录，如果登录放行，否则拒绝访问
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        UserInfo userInfo = (UserInfo) request.getSession().getAttribute("USER");
        if (userInfo == null) {
            //拒绝访问，返回错误提示
            Result result = Result.build("未登录，请先登录", ResultCodeEnum.LOGIN_AUTH);
//            JSON.toJSONString(result)
//            response.getWriter().println(result);
            WebUtil.writeJSON(response,result);
            return false;
        }

        return true;
    }

}
